package com.combetter.changxing.ui.activity;


import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;

import com.jakewharton.rxbinding2.view.RxView;
import com.combetter.changxing.R;
import com.combetter.changxing.databinding.ActivityLoginBinding;
import com.combetter.changxing.ui.base.BaseActivity;
import com.combetter.changxing.viewmodel.LoginViewModel;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.HashMap;
import java.util.Map;


/**
 * 登录Activity
 * <p>
 * Created by 深圳唐龙飞讯科技有限公司 on 2018/6/6.
 */

public class LoginActivity extends BaseActivity<ActivityLoginBinding, LoginViewModel> {
    private boolean isOpen = true;
    private String mOpenid;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addStatusBar();
    }

    @Override
    public void addStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { //透明状态栏
            View decorView = getWindow().getDecorView();
            //让应用主题内容占用系统状态栏的空间,注意:下面两个参数必须一起使用 stable 牢固的
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            //设置状态栏颜色为透明
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
    }

    @Override
    public int setLayout() {
        return R.layout.activity_login;
    }

    @Override
    public void init() {
        mBinding.ivShow.setOnClickListener(v -> {
            if (isOpen) {
                isOpen = false;
                mBinding.ivShow.setImageResource(R.mipmap.mima_kejian);
                //明文
                mBinding.etPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            } else {
                isOpen = true;
                mBinding.ivShow.setImageResource(R.mipmap.mima_bukejian);
                //密文
                mBinding.etPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
            }
            mBinding.etPwd.setSelection(mBinding.etPwd.getText().toString().trim().length());//将光标移至文字末尾
        });
    }

    @Override
    public Class createViewModel() {
        return LoginViewModel.class;
    }

    @Override
    public void loadData() {
        onClick();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        UMShareAPI.get(this).release();
    }


    private void onClick() {
        RxView.clicks(mBinding.ivQq).subscribe(o -> qqLogin());
        RxView.clicks(mBinding.ivWechat).subscribe(o -> weiXinLogin());
    }

    /**
     * 微信授权登录
     */
    public void weiXinLogin() {
//        authorization(SHARE_MEDIA.WEIXIN);
    }

    /**
     * QQ授权登录
     */
    public void qqLogin() {
//        authorization(SHARE_MEDIA.QQ);
    }

    //授权
    private void authorization(SHARE_MEDIA share_media) {
        UMShareAPI.get(this).getPlatformInfo(this, share_media, new UMAuthListener() {
            @Override
            public void onStart(SHARE_MEDIA share_media) {
                Log.d("----", "onStart " + "授权开始");
            }

            @Override
            public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
                Log.d("----", "onComplete " + "授权完成");

                //sdk是6.4.4的,但是获取值的时候用的是6.2以前的(access_token)才能获取到值,未知原因
                String uid = map.get("uid");
                String openid = map.get("openid");//微博没有
                String unionid = map.get("unionid");//微博没有
                String access_token = map.get("access_token");
                String refresh_token = map.get("refresh_token");//微信,qq,微博都没有获取到
                String expires_in = map.get("expires_in");
                String name = map.get("name");
                String gender = map.get("gender");
                String iconurl = map.get("iconurl");
//                Toast.makeText(getApplicationContext(), "openid = " + openid + "uid = " + uid + "name=" + name + ",gender=" + gender, Toast.LENGTH_SHORT).show();

                //拿到信息去请求登录接口。。。

                if (!TextUtils.isEmpty(openid)) {
                    mOpenid = openid;
                    mViewModel.nickname.set(name);
                    mViewModel.figureurl.set(iconurl);

                    HashMap<String, String> map1 = new HashMap<>();
                    map1.put("openId", openid);
                    mViewModel.login(LoginActivity.this, map1);
                }
            }

            @Override
            public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
                Log.d("----", "onError " + "授权失败");
            }

            @Override
            public void onCancel(SHARE_MEDIA share_media, int i) {
                Log.d("----", "onCancel " + "授权取消");
            }

        });
    }


    /**
     //     * QQ登录回调
     //     */
//    IUiListener listener = new IUiListener() {
//        @Override
//        public void onComplete(Object value) {
//            try {
//                if (value == null) {
//                    return;
//                }
//                JSONObject jo = (JSONObject) value;
//                int ret = jo.getInt("ret");
//                if (ret == 0) {
//                    String openID = jo.getString("openid");
//                    String accessToken = jo.getString("access_token");
//                    String expires = jo.getString("expires_in");
//                    mViewModel.mTencent.setOpenId(openID);
//                    mViewModel.mTencent.setAccessToken(accessToken, expires);
//                    //获取用户信息
//                    UserInfo info = new UserInfo(LoginActivity.this, mViewModel.mTencent.getQQToken());
//                    info.getUserInfo(requestListener);
//                }
//            } catch (JSONException e) {
//                e.printStackTrace();
//            }
//
//
//        }
//
//        @Override
//        public void onError(UiError uiError) {
//            LogUtil.e("QQ登录 onError " + uiError.errorMessage + "  errorCode = " + uiError.errorCode);
//        }
//
//        @Override
//        public void onCancel() {
//
//        }
//    };
//
//    /**
//     * QQ登录获取个人资料回调
//     */
//    IUiListener requestListener = new IUiListener() {
//        @Override
//        public void onComplete(Object value) {
//            try {
//                if (value == null) {
//                    return;
//                }
//                JSONObject jo = (JSONObject) value;
//                int ret = jo.getInt("ret");
//                if (ret == 0) {
//                    String nickname = jo.getString("nickname");
//                    String figureurl = jo.getString("figureurl");
//                    mViewModel.nickname.set(nickname);
//                    mViewModel.figureurl.set(figureurl);
//                    HashMap<String, String> map = new HashMap<>();
//                    map.put("openId", mViewModel.mTencent.getOpenId());
//                    mViewModel.login(LoginActivity.this, map);
//                }
//            } catch (JSONException e) {
//                e.printStackTrace();
//            }
//
//        }
//
//        @Override
//        public void onError(UiError uiError) {
//            LogUtil.e("QQ登录 获取资料 onError " + uiError.errorMessage + "  errorCode = " + uiError.errorCode);
//        }
//
//        @Override
//        public void onCancel() {
//
//        }
//    };
}
